package workers;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.ibm.mq.MQQueueManager;

import model.Software;
import workerUIs.StepFrame;
import workflow.step.ArchitectureDesignStep;

/**
 * ArchitectureStepWorker class.
 * 
 * @author Mark Czotter
 * @author Miklos Szennai
 * 
 */
public class ArchitectureStepWorker extends AbstractStepSwingWorker {
	
	private final String uiQueueName;

	public ArchitectureStepWorker(StepFrame frame, String inputQueueName,
			String outputQueueName, String uiQueueName) {
		super(frame, inputQueueName, outputQueueName);
		this.uiQueueName = uiQueueName;
	}
	

	
	@Override
	protected Software doInBackground() throws Exception {
		try {
			final MQQueueManager qm = new MQQueueManager(IMQConstants.MQM_NAME);
			this.software = getMessage(qm, inputQueueName);
			if (this.software != null) {
				doStep(this.software);
				putMessage(qm, outputQueueName, this.software);
				putMessage(qm, uiQueueName, this.software);
			}
			qm.disconnect();
			return this.software;
		} catch (Exception e) {
			logger.log(Level.SEVERE, "Exception when executing step: " + getClass().getName() + ": " + e.getMessage());
		}
		return null;
	}
	
	@Override
	protected void doStep(Software software) {
		new ArchitectureDesignStep().doStep(software);
	}
	
	public static void main(String[] args) {
		Logger logger = Logger.getLogger(ArchitectureStepWorker.class.getCanonicalName());
		try
		{
			final StepFrame architectureFrame = new StepFrame("Step2: Architecure Design", 400, 200);
			architectureFrame.getNextButton().addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					new ArchitectureStepWorker(architectureFrame, IMQConstants.SPEC_OUTPUT_ARCH_INPUT, IMQConstants.ARCH_OUTPUT_IMPL_INPUT, IMQConstants.ARCH_OUTPUT_UI_INPUT).execute();				
				}
			});
			new QueueDepthObserver().observeQueueDepth(architectureFrame, IMQConstants.SPEC_OUTPUT_ARCH_INPUT);
		}
		catch (Exception e)
		{
			logger.log(Level.SEVERE, "Error during initialization: ", e);
		}
	}

}
